/**
 * 
 */
package org.waddy.framework.test.rule;

import java.io.File;

import org.apache.log4j.Logger;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.waddy.framework.pub.util.db.DbTools;
import org.waddy.framework.test.annotation.AfterMethod;
import org.waddy.framework.test.annotation.BeforeMethod;

/**
 * @Title OperateRule.java
 * @version 1.0
 * @author zhangdapeng
 * @Created 2013-6-7
 * Copyright (c) 2013 PanGene InfoAnalytics.
 *
 * @Description: 
 */
public class OperateRule implements TestRule {
	private static final Logger logger = Logger.getLogger(OperateRule.class);

	private static final String scriptPrefix = "src/test/java/";
	
	/**
	 * 初始化数据
	 */
	public void initData(Class testClazz){
		logger.debug("init data ...");
		//初始化dbTools
		DbTools dbTools = new DbTools();
		dbTools.init();
		//构造sql脚本名称
		String packageName = testClazz.getPackage().getName();
		String sqlScript = scriptPrefix+packageName.replaceAll("\\.", "\\/");
		String className = testClazz.getSimpleName();
		sqlScript = sqlScript + "/" + className+".sql";
		//设置sql脚本文件
		File file = new File(sqlScript);
		if(!file.exists()){
			logger.debug("no the script.");
			return;
		}
		dbTools.setSrc(file);
		//执行sql脚本
		try {
			dbTools.execute();
		} catch (Exception e) {
			logger.debug("init data falid: ",e);
		}
	}

	/* (non-Javadoc)
	 * @see org.junit.rules.TestRule#apply(org.junit.runners.model.Statement, org.junit.runner.Description)
	 */
	@Override
    public Statement apply(final Statement base, final Description des) {
        return new Statement() {  
            @Override  
            public void evaluate() throws Throwable {  
                // Before method  
            	BeforeMethod before = des.getAnnotation(BeforeMethod.class);
            	String msg = null;
                if (before != null) {
                	msg = before.message();
                    System.out.println("\t--- begin test "+des.getMethodName()+":"+before.message()+" ---");
                }else{
                	System.out.println("\t--- begin test "+des.getMethodName()+" ---");
                }
                // 当目标方法指定需要initdb时才进行数据库初始化操作
                if("initdb".equalsIgnoreCase(msg)){
                	initData(des.getTestClass());
                }
				// Execute method
                base.evaluate();
                // After method  
                if (des.getAnnotation(AfterMethod.class) != null) {  
                    System.out.println(((AfterMethod) des  
                            .getAnnotation(AfterMethod.class)).message());  
                }
            }  
  
        }; 
//        // Before method  
//    	BeforeMethod before = des.getAnnotation(BeforeMethod.class); 
//        if (before != null) {  
//            System.out.println(before.message());  
//        }  
//        return base;
    }

}
